package org.kumas.bigdata.hdfs3.fsdirectory;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*************************************************
 * TODO_Kumas
 *  Author： KumasZhang
 *  DateTime： 2021-12-02$ 8:30$
 *  Description： INode ID和INode的关系维护
 **/
public class INodeMap {
    private static Map<String, INode> iNodeMap = new ConcurrentHashMap<>();
    private static final INodeMap INSTANCE = new INodeMap();

    private INodeMap() {
    }

    public static INodeMap getInstance() {
        if (iNodeMap.get("/") == null) {
            iNodeMap.put("/", new INodeDirectory("/", null));
        }
        return INSTANCE;
    }
    public final void put(INode inode) {
        iNodeMap.put(inode.getAbsolutePath(), inode);
    }

    public final void remove(INode inode) {
        iNodeMap.remove(inode.getAbsolutePath());
    }

    public int size() {
        return iNodeMap.size();
    }

    public INode get(String path) {
        return iNodeMap.get(path);
    }

    public void clear() {
        iNodeMap.clear();
    }

    Iterator<String> iterator() {
        return iNodeMap.keySet().iterator();
    }
}
